package com.zhkc.iess.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhkc.iess.system.dto.SysUserAddDTO;
import com.zhkc.iess.system.dto.SysUserQueryDTO;
import com.zhkc.iess.system.dto.SysUserResetPwdDTO;
import com.zhkc.iess.system.entity.SysResource;
import com.zhkc.iess.system.entity.SysUser;
import com.zhkc.iess.system.mapper.SysResourceMapper;
import com.zhkc.iess.system.mapper.SysUserMapper;
import com.zhkc.iess.system.vo.MetaVO;
import com.zhkc.iess.system.vo.SysResourcePermissionVO;
import com.zhkc.iess.system.vo.SysUserVO;
import com.zhkc.wisdom.common.base.util.SecurityUtils;
import com.zhkc.wisdom.common.pojo.vo.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 14567
 * @description 针对表【sys_user(用户信息表)】的数据库操作Service实现
 * @createDate 2024-12-16 17:35:38
 */
@Service
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser>
        implements IService<SysUser> {
    @Autowired
    private SysUserMapper mapper;

    @Autowired
    private SysResourceMapper resourceMapper;

    public PageVO<SysUserVO> pageList(SysUserQueryDTO dto) {
        Page<SysUser> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<SysUser>().eq(StringUtils.isNotBlank(dto.getAccount()), SysUser::getAccount, dto.getAccount());
        Page<SysUser> pageList = this.page(page, wrapper);
        return new PageVO<>(pageList, SysUserVO.class);
    }


    public Long create(SysUserAddDTO dto) {
        return null;
    }


    public Void update(Long id, SysUserAddDTO dto) {
        return null;
    }


    public Void delete(Long id) {
        return null;
    }


    public Void updateStatus(Long id) {
        return null;
    }


    public Void resetPwd(Long id, SysUserResetPwdDTO dto) {
        return null;
    }


    public SysUserVO listByRole(String roleCode) {
        return null;
    }

    public List<SysResourcePermissionVO> getResource() {
        List<SysResource> list = this.resourceMapper.getResourcePermission(SecurityUtils.getAccountId());
        if(CollectionUtil.isEmpty(list)){
            return null;
        }
        List<SysResourcePermissionVO> allList = new ArrayList<>();
        //对象转换
        list.forEach(item ->{
            SysResourcePermissionVO model = new SysResourcePermissionVO();
            model.setName(item.getResourceCode());
            model.setPath(item.getPath());
            model.setHidden(!"0".equals(item.getResourceStatus()));
            if ("3".equals(item.getResourceType())){
                model.setAlwaysShow(true);
                model.setRedirect("noRedirect");
                model.setComponent("Layout");
            }else if ("1".equals(item.getResourceType())){
                model.setComponent(item.getResourceUrl());
            }
            model.setPid(item.getPid());
            model.setId(item.getId());
            MetaVO metaVO = new MetaVO();
            metaVO.setTitle(item.getResourceName());
            metaVO.setIcon(item.getResourceIcon());
            metaVO.setNoCache(false);
            metaVO.setLink(null);
            model.setMeta(metaVO);
            allList.add(model);
        });


//        //找出父级菜单
//        List<SysResourcePermissionVO> pidList = allList.stream().filter(item -> item.getPid() == 0).toList();
//        list.removeAll(pidList);
//        //递归
//        resourceDg(pidList,allList);

        return allList;
    }

    private void resourceDg(List<SysResourcePermissionVO> pidList,List<SysResourcePermissionVO> list){
        pidList.forEach(p ->{
            List<SysResourcePermissionVO> childList = list.stream().filter(item -> Objects.equals(item.getPid(), p.getId())).toList();
            if (CollectionUtil.isNotEmpty(childList)){
                p.setChildren(childList);
                list.removeAll(childList);
                resourceDg(childList,list);
            }
        });
    }
}




